package com.itrus.util.sign;

import com.itrus.util.Base64;
import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;

public class RSAWithSoftware
{
  public static final String KEY_ALGORITHM = "RSA";
  private static final int MAX_DECRYPT_BLOCK = 128;
  private static final int MAX_ENCRYPT_BLOCK = 116;
  private static final String PRIVATE_KEY = "RSAPrivateKey";
  private static final String PUBLIC_KEY = "RSAPublicKey";
  public static final String SIGNATURE_ALGORITHM = "MD5withRSA";

  public static String decryptByPrivateKey(String paramString1, String paramString2)
    throws Exception
  {
    byte[] arrayOfByte1 = Base64.decode(paramString2);
    byte[] arrayOfByte2 = Base64.decode(paramString1);
    PKCS8EncodedKeySpec localPKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(arrayOfByte1);
    KeyFactory localKeyFactory = KeyFactory.getInstance("RSA");
    PrivateKey localPrivateKey = localKeyFactory.generatePrivate(localPKCS8EncodedKeySpec);
    Cipher localCipher = Cipher.getInstance(localKeyFactory.getAlgorithm());
    localCipher.init(2, localPrivateKey);
    int i = arrayOfByte2.length;
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    int j = 0;
    int k = 0;
    if (i - j <= 0)
    {
      byte[] arrayOfByte4 = localByteArrayOutputStream.toByteArray();
      localByteArrayOutputStream.close();
      return new String(arrayOfByte4, "utf-8");
    }
    if (i - j > 128);
    for (byte[] arrayOfByte3 = localCipher.doFinal(arrayOfByte2, j, 128); ; arrayOfByte3 = localCipher.doFinal(arrayOfByte2, j, i - j))
    {
      localByteArrayOutputStream.write(arrayOfByte3, 0, arrayOfByte3.length);
      k++;
      j = k * 128;
      break;
    }
  }

  public static String encryptByPublicKey(String paramString1, String paramString2)
    throws Exception
  {
    byte[] arrayOfByte1 = Base64.decode(paramString2);
    byte[] arrayOfByte2 = paramString1.getBytes("utf-8");
    X509EncodedKeySpec localX509EncodedKeySpec = new X509EncodedKeySpec(arrayOfByte1);
    KeyFactory localKeyFactory = KeyFactory.getInstance("RSA");
    PublicKey localPublicKey = localKeyFactory.generatePublic(localX509EncodedKeySpec);
    Cipher localCipher = Cipher.getInstance(localKeyFactory.getAlgorithm());
    localCipher.init(1, localPublicKey);
    int i = arrayOfByte2.length;
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    int j = 0;
    int k = 0;
    if (i - j <= 0)
    {
      byte[] arrayOfByte4 = localByteArrayOutputStream.toByteArray();
      localByteArrayOutputStream.close();
      return Base64.encode(arrayOfByte4).replaceAll("\n", "").replaceAll("\r\n", "").replaceAll("\r", "");
    }
    if (i - j > 116);
    for (byte[] arrayOfByte3 = localCipher.doFinal(arrayOfByte2, j, 116); ; arrayOfByte3 = localCipher.doFinal(arrayOfByte2, j, i - j))
    {
      localByteArrayOutputStream.write(arrayOfByte3, 0, arrayOfByte3.length);
      k++;
      j = k * 116;
      break;
    }
  }

  public static Map<String, Object> genKeyPair()
    throws Exception
  {
    KeyPairGenerator localKeyPairGenerator = KeyPairGenerator.getInstance("RSA");
    localKeyPairGenerator.initialize(1024);
    KeyPair localKeyPair = localKeyPairGenerator.generateKeyPair();
    RSAPublicKey localRSAPublicKey = (RSAPublicKey)localKeyPair.getPublic();
    RSAPrivateKey localRSAPrivateKey = (RSAPrivateKey)localKeyPair.getPrivate();
    HashMap localHashMap = new HashMap(2);
    localHashMap.put("RSAPublicKey", localRSAPublicKey);
    localHashMap.put("RSAPrivateKey", localRSAPrivateKey);
    return localHashMap;
  }

  public static String getPrivateKey(Map<String, Object> paramMap)
    throws Exception
  {
    return Base64.encode(((Key)paramMap.get("RSAPrivateKey")).getEncoded()).replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", "");
  }

  public static String getPublicKey(Map<String, Object> paramMap)
    throws Exception
  {
    return Base64.encode(((Key)paramMap.get("RSAPublicKey")).getEncoded()).replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", "");
  }

  public static String signByPrivateKey(String paramString1, String paramString2)
    throws Exception
  {
    PKCS8EncodedKeySpec localPKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decode(paramString2));
    PrivateKey localPrivateKey = KeyFactory.getInstance("RSA").generatePrivate(localPKCS8EncodedKeySpec);
    Signature localSignature = Signature.getInstance("MD5withRSA");
    localSignature.initSign(localPrivateKey);
    localSignature.update(paramString1.getBytes("utf-8"));
    return Base64.encode(localSignature.sign()).replaceAll("\n", "").replaceAll("\r\n", "").replaceAll("\r", "");
  }

  public static boolean validateSignByPublicKey(String paramString1, String paramString2, String paramString3)
    throws Exception
  {
    X509EncodedKeySpec localX509EncodedKeySpec = new X509EncodedKeySpec(Base64.decode(paramString2));
    PublicKey localPublicKey = KeyFactory.getInstance("RSA").generatePublic(localX509EncodedKeySpec);
    Signature localSignature = Signature.getInstance("MD5withRSA");
    localSignature.initVerify(localPublicKey);
    localSignature.update(paramString1.getBytes("utf-8"));
    return localSignature.verify(Base64.decode(paramString3));
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.itrus.util.sign.RSAWithSoftware
 * JD-Core Version:    0.6.2
 */